package com.sqx.modules.cityInfo.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.cityInfo.entity.CityInfo;
import com.sqx.modules.cityInfo.service.CityInfoService;
import com.sqx.modules.cityInfo.vo.CityInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 城市-管理端
 * @author wuChang
 * @since 2024-01-23
 */
@Slf4j
@RestController
@Api(value = "城市-管理端", tags = {"城市-管理端"})
@RequestMapping("/admin/cityInfo/")
public class CityInfoController {
    @Autowired
    private CityInfoService cityInfoService;
    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    /**
     * 新增或修改城市
     *@param cityInfo 城市
     */
    @ApiOperation("新增或修改城市")
    @PostMapping(value = "saveCityInfo")
    public Result saveCityInfo(CityInfo cityInfo) {
        return cityInfoService.saveCityInfo(cityInfo);
    }


    /**
     * 获取城市列表
     * @param cityInfo 城市
     */
    @ApiOperation("获取城市列表")
    @GetMapping(value = "getCityInfoList")
    public Result getCityInfoList(Integer page, Integer limit, CityInfo cityInfo) {

        return Result.success().put("data", cityInfoService.getCityInfoList(page, limit, cityInfo));
    }

    /**
     * 开关/关闭城市服务
     * @param id 城市id
     */
    @ApiOperation("开关/关闭城市服务")
    @GetMapping(value = "updateEnable")
    public Result updateEnable(Long id) {
        return cityInfoService.updateEnable(id);
    }

    /**
     * 删除城市
     *@param id 城市id
     */
    @ApiOperation("删除城市")
    @PostMapping(value = "deleteCityInfoById")
    public Result deleteCityInfoById(Long id) {

        return cityInfoService.removeById(id) ? Result.success() : Result.error();
    }

    /**
     * 获取城市详情
     *@param id 城市id
     */
    @ApiOperation("获取城市详情")
    @GetMapping(value = "getCityInfoById")
    public Result getCityInfoById(Long id) {
        return Result.success().put("data", cityInfoService.getById(id));
    }


    @ApiOperation("设置代理城市")
    @GetMapping(value = "setAgentCity")
    public Result setAgentCity(Long id, Long userId) {
        reentrantReadWriteLock.writeLock().lock();
        try {
            return cityInfoService.setAgentCity(id, userId);
        } catch (Exception e) {
            log.error("设置代理商异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("设置代理商异常,请重试");

    }

    @ApiOperation("代理商修改城市分佣比例")
    @PostMapping(value = "updateCityRate")
    public Result updateCityRate(CityInfo cityInfo) {
        return cityInfoService.updateCityRate(cityInfo);
    }

    @ApiOperation("获取所有已有代理的市")
    @GetMapping(value = "getHasAgentCityList")
    public Result getHasAgentCityList(Integer page, Integer limit, CityInfoVo cityInfoVo){
        return Result.success().put("data", cityInfoService.getHasAgentCityList(page,limit,cityInfoVo));
    }
}